package com.example.communitypro.service.impl;

import com.example.communitypro.mapper.UserResourceMapper;
import com.example.communitypro.pojo.UserResource;
import com.example.communitypro.service.UserResourceService;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserResourceServiceImpl implements UserResourceService {
    @Autowired(required=false)
    private UserResourceMapper userResourceMapper;


    @Override
    public Result addUserResource(UserResource record){
        try {
            if(userResourceMapper.insertSelective(record)>0)
            {
                return Result.ok().message("新增用户资源成功");
            }
            else{
                return Result.error().message("新增用户资源失败");
            }
        }catch (Exception e){
            return Result.error().message("新增用户资源失败");
        }
    }


    @Override
    public Result dropUserResource(int UserResourceNo){
        try {
            if(userResourceMapper.deleteByPrimaryKey(UserResourceNo)>0)
            {
                return Result.ok().message("删除用户资源成功");
            }
            else{
                return Result.error().message("删除用户资源失败");
            }
        }catch (Exception e){
            return Result.error().message("删除用户资源失败");
        }
    }

    @Override
    public Result modifyUserResource(UserResource record){
        try {
            if(userResourceMapper.updateByPrimaryKeySelective(record)>0)
            {
                return Result.ok().message("修改成功");
            }
            else{
                return Result.error().message("修改失败：未找到对应记录");
            }
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应记录");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }
    }

    @Override
    public Result queryUserResource(int userResourceNo){
        try {
            UserResource userResource=userResourceMapper.selectByPrimaryKey(userResourceNo);
            if(userResource!=null)
            {
                return Result.ok().message("查找用户资源成功").data("userResource",userResource);
            }
            else{
                return Result.error().message("查找用户资源失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户资源失败");
        }
    }

    @Override
    public Result queryAllUserResource(){
        try {
            List<UserResource> userResources=userResourceMapper.findAll();
            if(userResources!=null)
            {
                return Result.ok().message("查找所有用户资源成功").data("userResource",userResources);
            }
            else{
                return Result.error().message("查找所有用户资源失败");
            }
        }catch (Exception e){
            return Result.error().message("查找所有用户资源失败");
        }
    }

    @Override
    public Result quertUserResourceByUserResourceNo(int user_resource_no){
        try {
            UserResource userResource=userResourceMapper.findByUserResourceNo(user_resource_no);
            if(userResource!=null)
            {
                return Result.ok().message("查找用户资源成功").data("userResource",userResource);
            }
            else{
                return Result.error().message("查找用户资源失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户资源失败");
        }
    }
}
